window.onload = function () {
  var messagesDiv = document.getElementById('messages');
  var ws = new WebSocket('ws://localhost:8080');

  ws.onopen = function () {
    console.log('Connected to WebSocket server.');
  };

  ws.onmessage = function (event) {
    var messageElement = document.createElement('p');
    messageElement.textContent = event.data;
    messagesDiv.appendChild(messageElement);
  };

  ws.onerror = function (event) {
    console.error('WebSocket error observed:', event);
  };

  ws.onclose = function (event) {
    console.log('WebSocket is closed now.');
  };
};